package com.example.game_community.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.game_community.entity.Comment;
import com.example.game_community.entity.Post;
import com.example.game_community.service.PostService;
import com.example.game_community.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping("/posts")
public class PostController {

    @Autowired
    private PostService postService;

    @Autowired
    private ServletWebServerApplicationContext servletContext;

    // 创建贴子
    @PostMapping("/createPost")
    public Result<Post> createPost(@RequestBody Post post) {
        return postService.createPost(post);
    }

    //获取全部贴子
    @GetMapping("/getAllPost")
    public Result<Page<Post>> getAllPost(@RequestParam int current, @RequestParam int size) {
        return postService.getAllPost(current, size);
    }


    // 获取详情帖子
    @GetMapping("/postId={postId}")
    public Result<Post> getPosts(@PathVariable Long postId) {

        return postService.getPostDetail(postId);
    }

    @GetMapping("/post/authorId={userId}")
    public Result<List<Post>> getUserPosts(@PathVariable Long userId) {
        return postService.getUserPostDetail(userId);
    }

    // 添加回复
    @PostMapping("/{postId}/comments")
    public Result<Comment> createComment(@PathVariable Long postId, @RequestBody Comment comment) {

        return postService.createComment(postId, comment);
    }


    @PostMapping("/temp-upload")
    public Result<String> HandleUpload(@RequestParam("file") MultipartFile file) throws IOException {
      return  postService.getTempImgUrls(file);
    }
}
